package com.tj.jinzao.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.SearchFriendActivity;
import com.tj.jinzao.adapter.GroupAdapter;
import com.tj.jinzao.base.BaseFragment;
import com.tj.jinzao.callback.IListGroupView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.GroupBean;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.GroupPresenter;
import com.tj.jinzao.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import butterknife.BindView;

public class CreateGroupFragment extends BaseFragment<GroupPresenter> implements IListGroupView<GroupBean> {
    protected final static String ARG_TAG_ID = "tagId";
    protected final static String ARG_LOAD_ID = "load_id";

    @BindView(R.id.create)
    TextView create;
    @BindView(R.id.groupName)
    TextView groupName;
    @BindView(R.id.grid_lv)
    GridView grid_lv;
    private GroupAdapter groupAdapter;

    @Override
    protected GroupPresenter createPresenter() {
        user = UserManager.getInstance().getUser();
        return new GroupPresenter(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public static CreateGroupFragment newInstance() {
        CreateGroupFragment fragment = new CreateGroupFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, "1");
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.create_group);
            fragment.setArguments(args);
        }

        return fragment;
    }
    private ArrayList<String> list_friend=new ArrayList<>();
    private LinkedList<String> list_photo=new LinkedList<>();
    private String userFriend;
    private User user;

    @Override
    protected void setupViews(LayoutInflater inflater, View view) {
        list_photo.addLast("fin");
        groupAdapter=new GroupAdapter(getActivity(),list_photo,false);
        grid_lv.setAdapter(groupAdapter);
        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(list_friend.size()>0){
                    userFriend="";
                    for (int i=0;i<list_friend.size();i++){
                        if(i==0){
                            userFriend=list_friend.get(i);
                        }else {
                            userFriend=userFriend+","+list_friend.get(i);
                        }
                    }
                    if(TextUtils.isEmpty(groupName.getText())||groupName.getText().equals("")){
                        ToastUtils.showToast(getActivity(),"请输入群名称");
                    }else {
                        HashMap<String,Object> hashMap=new HashMap<>();
                        hashMap.put("name",groupName.getText().toString());
                        hashMap.put("ownId",user.getId());
                        hashMap.put("userIds",userFriend);
                        mPresenter.createGroup(hashMap);

                    }

                }else {
                    ToastUtils.showToast(getActivity(),"请至少选择一位好友");
                }

            }
        });
        grid_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(i==list_photo.size()-1){
                    Intent to=new Intent();
                    to.putExtra("type","2");
                    to.setClass(getActivity(), SearchFriendActivity.class);
                    startActivityForResult(to,101);

                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==Activity.RESULT_OK){
            switch (requestCode){
                case 101:
                    list_photo.clear();
                    list_friend.clear();
                    list_friend=data.getStringArrayListExtra("RESULT_DATA");
                    for (int i=0;i<data.getStringArrayListExtra("friendListPhoto").size();i++){
                        list_photo.add(data.getStringArrayListExtra("friendListPhoto").get(i));
                    }
                    list_photo.addLast("fin");
                    groupAdapter.setData(list_photo);
                    break;

            }

        }
    }

    @Override
    public void onCreateGroupSuccess(GroupBean friendBean) {
        ToastUtils.showToast(getActivity(),"创建成功");
        getActivity().finish();
    }

    @Override
    public void onFailed(ResultResponse response) {
//        if(response.getCode().equals("00")){
//            ToastUtils.showToast(getActivity(),"创建成功");
            getActivity().finish();
//        }else {
//            ToastUtils.showToast(getActivity(),response.getMessage());
//        }

    }

    @Override
    public void onRequestSuccess(GroupBean groupBean) {

    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }
}
